<?php
namespace Particle\Core;
use Particle\Core as Core;
/**
 *  @package Particle
 *  @category Core
 *  @author dertin
 * 
 **/

abstract class Addons
{   
    protected $_viewAddons = NULL;
    protected $_modelAddons = NULL;
    private static $_isLoadAddons = FALSE;
    private static $_addons_name = NULL;
    
    protected function __destruct() {
        self::$_isLoadAddons = FALSE;
        self::$_addons_name = NULL;
    }
    
    protected function __construct() {
        
        if(self::$_isLoadAddons == TRUE && isset(self::$_addons_name) && is_string(self::$_addons_name)){
            
            $this->_viewAddons = new Core\ViewAddons(self::$_addons_name, false);
            $this->_modelAddons = $this->loadModelAddons(FALSE, TRUE);
            
        }else{
            return FALSE;
        }
    }
    
    protected final function loadModelAddons($modelAddonsName, $noException=FALSE){
        
        $modelClass = $modelAddonsName . 'ModelAddons';
        $pathModel = ADDONS_PATH . $modelAddonsName . DS .'models'. DS . $modelClass.'.php';
        
        if (is_readable($pathModel)) {

            require_once $pathModel;
            
            $modelNamesapce =  'Particle\\Addons\\' . $modelClass;
   
            if (class_exists($modelNamesapce, FALSE)) {

                $modelInstance = new $modelNamesapce;
                              
                return $modelInstance;
            } else {

                if (!$noException) {

                        throw new \Exception('Bad Addons');
                                        
                } else {
                    return FALSE;
                }
                
            }
        } else {
            if (!$noException) {
                
                    throw new \Exception('Error Addons');
                
            } else {
                return FALSE;
            }
        }
    }
    
    public static function loadAddon($addons_name = FALSE, $noException = FALSE) {
        
        if($addons_name == FALSE || empty($addons_name)){
            return FALSE;
        }
        self::$_isLoadAddons = FALSE;
        self::$_addons_name = $addons_name;
        
        $pathPluginInit = ADDONS_PATH . $addons_name . DS .'init.php';
        
        if (is_readable($pathPluginInit)) {
            
            require_once $pathPluginInit;
            
            $classPlugin = $addons_name.'Addons';
            
            $pluginNamesapce =  'Particle\\Addons\\' . $classPlugin;
            
            if (class_exists($pluginNamesapce, FALSE)){
                
                self::$_isLoadAddons = TRUE;
                                
                $pluginInstance = new $pluginNamesapce;
                
                if(isset($pluginNamesapce) && is_object($pluginInstance)){
                    self::$_isLoadAddons = FALSE;
                    return $pluginInstance;
                }else{
                    return FALSE;
                }
                
            } else {
                
                if (!$noException) {
                    throw new \Exception('Bad Class Name Addons');
                } else {
                    return FALSE;
                }
                
            }
            
        } else {
            
            if (!$noException) {
                throw new \Exception('Error Addons');
            } else {
                return FALSE;
            }
            
        }
    }
}